package com.hand.demo.app.service.impl;

import com.hand.demo.app.service.MessageService;
import org.hzero.boot.message.MessageClient;
import org.hzero.boot.message.entity.Receiver;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yufan.yao@hand-china.com 2023/9/26 11:10
 */
public class MessageServiceImpl implements MessageService {
    /**
     * 指定接收组发送消息
     * @param tenantId   租户id
     * @param messageTemplateCode 消息模板编码
     * @param receiverGroupCode  指定接收人的用户ID，以及目标租户ID
     * @param args  消息模板参数
     * @return
     */
    @Autowired
    private MessageClient messageClient;

    @Override
    public void sendMessage() {
        long tenantId = 2L;
        String messageTemplateCode = "44200YYF";
        String lang = "zh_CN";
        Receiver receiver = new Receiver().setUserId(1L).setTargetUserTenantId(1L);
        Map<String, String> args = new HashMap<>(16);
        args.put("processName", "测试消息的发送");
        args.put("processDescription", "测试消息的发送");
        // 同步发送站内消息
        messageClient.sendWebMessage(tenantId, messageTemplateCode, lang, Collections.singletonList(receiver), args);
        // 异步发送站内消息
        messageClient.async().sendWebMessage(tenantId, messageTemplateCode, lang, Collections.singletonList(receiver),
                        args);
    }

    @Override
    public void sendEmail() {
        long tenantId = 2L;
        String serverCode = "yaoyufan-demo";
        String messageTemplateCode = "44200YYF";
        // 收件人
        Receiver receiver = new Receiver().setEmail("1518677071@qq.com");
        Map<String, String> args = new HashMap<>(16);
        args.put("processName", "测试消息");
        args.put("processDescription", "测试消息");
        // 同步发送站内消息
        messageClient.sendEmail(tenantId, serverCode, messageTemplateCode, Collections.singletonList(receiver), args);
        // 异步发送站内消息
        messageClient.async().sendEmail(tenantId, serverCode, messageTemplateCode, Collections.singletonList(receiver),
                        args);
    }
}
